from abaqusConstants import *
import abaqus_functions
import __main__
abaqus_functions = reload( abaqus_functions)
import os
myv = __main__.session.viewports['Viewport: 1'] 
__main__.session.graphicsOptions.setValues(backgroundStyle=SOLID, 
    backgroundColor='#FFFFFF')
def save_contour( cc, frame_num = None, sufix = 'change_sufix'):
    odb = cc.attach_results()
    prev = 0.
    if frame_num == None:
        for i in range( len(cc.zload) ):
            zload = cc.zload[i]
            if abs(zload) < abs(prev):
                break
            prev = zload
        frame_num = i-1
        sufix = 'first_buckling'
    else:
        frame_num = frame_num
        sufix = sufix
    myv.setValues(displayedObject=odb)
    myv.odbDisplay.setPrimaryVariable(
        variableLabel='U', outputPosition=NODAL, refinement=(INVARIANT, 
        'Magnitude'), )
    myv.odbDisplay.display.setValues( plotState=CONTOURS_ON_DEF )
    __main__.session.viewports[__main__.session.currentViewportName].odbDisplay.setFrame(
                                                    step='Axial_Displ_Step',
                                                    frame=frame_num)
    myv.restore()
    myv.view.setViewpoint(\
                            viewVector     = (0, 0, 1), 
                            cameraUpVector = (0, 1, 0),)
    myv.setValues(\
                    origin = (0.,0.), 
                    width  = 300,
                    height = 300,)
    myv.view.setValues(\
        width          = 800,
        height         = 800,
        nearPlane      = 2075.29,
        farPlane       = 2489.33,
        cameraPosition = (0, 0, 2356.81),
        cameraTarget   = (0, 0, 0),)
    contour_name = cc.jobname + '_' + sufix
    __main__.session.printToFile( fileName      = contour_name,
                         format        = PNG,
                         canvasObjects = (myv,))
    cc.detach_results( odb )
